package com.djinfo.work.dao.impl;

/**
 * Created by lenovo on 2017/5/17.
 */

import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import com.djinfo.work.dao.DepartmentDao;
import com.djinfo.work.entity.Department;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 部门
 */
@Repository("DepartmentDao")
public class DepartmentDaoImpl extends BaseDaoImpl implements DepartmentDao {

    @Override
    public void save(Department object) {
        if (object.getId() == null) {
            super.save(object);
        } else {
            super.update(object);
        }
    }

    @Override
    public void update(Department object) {
        super.update(object);
    }

    @Override
    public void delete(Department object) {
        Department department = (Department)super.getByPrimaryKey(Department.class,object.getId());
        super.delete(department);
    }


    @Override
    public Department getDepartmentById(String id) {
        return (Department)super.get(Department.class,id);
    }

    @Override
    public List<Department> page(Department department, Integer pageNum, Integer pageSize) {
        String hql="";
        if(department.getDeptName()==null||"".equals(department.getDeptName())){
            hql=" from Department where parentDeptId='"+department.getParentDeptId()+"'";
        }else{
            hql=" from Department where parentDeptId='"+department.getParentDeptId()+"' and deptName like '%"+ department.getDeptName()+ "%'";
        }
        return (List<Department>)super.pageQuery( hql,  pageNum,  pageSize);
    }

    @Override
    public List<Department> list() {
        String hql = " from Department where 1=1 ";
        return (List<Department>)super.getList(hql);
    }

    @Override
    public Integer getAllRowCount(Department department) {
        String hql="";
        if(department.getDeptName()==null||"".equals(department.getDeptName())){
            hql = "select count(*) from Department where parentDeptId='"+department.getParentDeptId()+"'  ";
        }else {
            hql = "select count(*) from Department where parentDeptId='"+department.getParentDeptId()+"' and deptName like '%"+ department.getDeptName()+ "%' ";
        }

        return super.getUniqueObject(hql).hashCode();
    }

    @Override
    public List<Department> getDepartsBy(String parentid) {
        String hql="";
        int length =parentid.length();
        if(length>2){
            hql = "from Department where parentDeptId='"+parentid+"'";
        }else{
            hql = "from Department where parentDeptId='"+0+"'";
        }
        return (List<Department>)super.getList(hql);
    }

    @Override
    public List<Department> getTwoDepartid() {
        String hql="from Department where parentDeptId !='"+0+"'";
        return (List<Department>)super.getList(hql);
    }

    @Override
    public List<Department> getOneDepartid() {
        String hql="from Department where parentDeptId ='"+0+"'";
        return (List<Department>)super.getList(hql);
    }

    @Override
    public List<Department> setTwoToOne(String deptid) {
        String hql="from Department where parentDeptId ='"+deptid+"'";
        return (List<Department>)super.getList(hql);
    }

    @Override
    public List<Department> getLevel1DepartList() {
        String hql="from Department where parentDeptId ='"+0+"'";
        return (List<Department>)super.getList(hql);
    }

    @Override
    public List<Department> getLevel2DepartList() {
        String hql="from Department where parentDeptId !='"+0+"'";
        return (List<Department>)super.getList(hql);
    }

    @Override
    public void deleteChildByParentDepartId(String pid) {
        String hql = " delete FROM  Department where parentDeptId = ? ";
        super.updateByHQL(hql,pid);
    }

    @Override
    public List<Department> getDeptListByParentId(String pid) {
        String hql="from Department where parentDeptId = ? ";
        return (List<Department>)super.getList(hql,pid);
    }
}
